package chat_api

import (
	"gin_study_blog/common"
	"gin_study_blog/common/blog_type"
	"gin_study_blog/common/http"
	"gin_study_blog/models"
	"github.com/gin-gonic/gin"
)

type ChatResponse struct {
	NickName string            `json:"nick_name"`
	Avatar   string            `json:"avatar"`
	Content  string            `json:"content"`  // 聊天内容
	IsGroup  bool              `json:"is_group"` // 是否为群组聊天 true是 false否
	MsgType  blog_type.MsgType `json:"msg_type"` // 聊天类型
}

func (ChatApi) ChatListView(c *gin.Context) {
	var cr models.PageInfo
	err := c.ShouldBindQuery(&cr)
	if err != nil {
		http.FailWithCode(http.ParamsError, c)
		return
	}

	chatData := make([]*ChatResponse, 0)
	page := (cr.Page - 1) * cr.Limit
	var count int64
	err = common.DB.Model(models.ChatModel{}).
		Select("nick_name", "avatar", "content", "is_group", "msg_type").
		Where("is_group = ?", true).
		Count(&count).Offset(page).
		Limit(cr.Limit).Scan(&chatData).Error
	if err != nil {
		common.Log.Error(err)
		http.FailWithMessage("系统错误", c)
		return
	}
	http.IsOkWithList(chatData, count, c)
}
